﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ToDo.Data;
using ToDo.Model;


namespace ToDo
{
    /// <summary>
    /// MyDayPage.xaml 的交互逻辑
    /// </summary>
    public partial class MyDayPage : Page
    {
        ArrayList myDays = new ArrayList();
        private int count;

        public int Count
        {
            get { return count; }
            set { count = value; }
        }
        private string date;

        public string DateNoew
        {
            get { return date; }
            set { date = value; }
        }


        public MyDayPage()
        {
            InitializeComponent();
            loadDate();
            showDate();
        }

        public void showDate() {
            TB_Date.Text = DateTime.Now.ToString();
        }

        public void loadDate()
        {
            IV_MyDay.Visibility = Visibility.Collapsed;
            using (SqlConnection connection = new SqlConnection(LinkSqlServer.source))
            {
                connection.Open();
                SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM MYDAY", connection);
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet, "MYDAY");
                foreach (DataRow row in dataSet.Tables["MYDAY"].Rows)
                {
                    if ((int)row[2] == 0)
                    {
                        myDays.Add(new MyDayModel() { Message = (string)row[1], ID = (int)row[0],Icon = "\xe61a" ,Indicator = "#140006" });
                        }
                    else
                    {
                        myDays.Add(new MyDayModel() { Message = (string)row[1], ID = (int)row[0], Icon = "\xe68b",Indicator = "#FF7373" });
                    }
                    Count = (int)row[0] +1;
                }
                if (myDays.Count > 0)
                {
                    IV_MyDay.Visibility = Visibility.Collapsed;
                    LV_myDy.Visibility = Visibility.Visible;
                    LV_myDy.ItemsSource = myDays;
                }
                else
                {
                    LV_myDy.Visibility = Visibility.Collapsed;
                    IV_MyDay.Visibility = Visibility.Visible;
                }
            }

            }


        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                myDays.Clear();
                LinkSqlServer.Add("INSERT INTO MYDAY VALUES(" + Count + ",'" + TB_Input.Text.ToString() + "',0,'"+DateTime.Now.ToString()+"')");
                update();
            }
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            string sqlCommand = "UPDATE MYDAY SET ISFINISH = 1 WHERE ID =" +LV_myDy.SelectedIndex;
            using (SqlConnection connection = new SqlConnection(LinkSqlServer.source))
            {
                connection.Open();
                SqlCommand command = new SqlCommand(sqlCommand,connection);
                int number = command.ExecuteNonQuery();
                if (number == 1)
                {
                    myDays.Clear();
                    update();
                }
            }
        }

        public void update()
        {
            loadDate();
            LV_myDy.ItemsSource = null;
            LV_myDy.ItemsSource = myDays;
            TB_Input.Text = "";
        }


        private void TB_Input_MouseEnter(object sender, MouseEventArgs e)
        {
            TB_Input.Text = "";
        }

        private void TB_Input_MouseLeave(object sender, MouseEventArgs e)
        {
            TB_Input.Text = "添加任务";
        }

        private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }
    }
}
